package com.xy.blog.article.domain.dao.impl;


import com.xy.blog.article.domain.dao.IArticlePicDao;
import com.xy.blog.article.domain.dao.mapper.ArticlePicMapper;
import com.xy.blog.article.domain.dao.model.ArticlePicDO;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/6/3
 */
@Repository
public class IArticlePicDaoImpl implements IArticlePicDao {

    @Autowired
    private ArticlePicMapper articlePicMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ArticlePicDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        LocalDateTime now = LocalDateTime.now();
        item.setCreateTime(now);
        item.setUpdateTime(now);
        articlePicMapper.insert(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ArticlePicDO item) throws BaseException {
        articlePicMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) throws BaseException {
        articlePicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ArticlePicDO getById(Long id) throws BaseException {
        return articlePicMapper.selectByPrimaryKey(id);
    }
}
